package com.fkdx.webman.controller;


import com.fkdx.common.dto.ResponseDTO;
import com.fkdx.common.dto.sys.CurrentUserDTO;
import com.fkdx.common.sys.SecurityHelper;
import com.fkdx.dao.entity.PageResult;
import com.fkdx.service.UserService;
import com.fkdx.service.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户表controller
 */
@RestController
@RequestMapping("/user")
@Log4j2
@Api(tags = "用户管理")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping(value = "/permission/list")
    @ApiOperation(value = "获取登录权限列表", response = PermissionQueryResp.class, httpMethod = "GET")
    public List<PermissionQueryResp> commonPrmissionList() {
        return userService.getUserPermission();
    }

    @GetMapping(value = "/role/all")
    @ApiOperation(value = "获取登录权限列表", response = PermissionQueryResp.class, httpMethod = "GET")
    public List<RoleQueryResp> roleALL() {
        return userService.roleALL();
    }

    @GetMapping("/list")
    @ApiOperation(value = "用户分页列表")
    public PageResult<UserQueryResp> getUserList(UserQueryReq req) {
        return userService.getUserList(req);
    }

    @GetMapping("/all")
    public List<UserQueryResp> getAll(UserQueryReq req) {
        return userService.getAll(req);
    }

    @GetMapping("/form")
    @ApiOperation(value = "根据id获取用户")
    public UserQueryResp getUserForm(Long id) {
        return userService.getUserForm(id);
    }

    @GetMapping("/integral")
    @ApiOperation(value = "获取积分")
    public ResponseDTO<String> getIntegral(@Param("userName") String userName) {
        CurrentUserDTO principal = SecurityHelper.getPrincipal();
        if (!principal.getUserName().equals(userName)) {
            return ResponseDTO.buildSuccessDataResp("0");
        }
        Integer integral = userService.getIntegral(userName);
        return ResponseDTO.buildSuccessDataResp(integral.toString());
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增修改用户")
    public ResponseDTO<String> saveUserForm(@RequestBody @Valid UserReq req) {
        CurrentUserDTO principal = SecurityHelper.getPrincipal();
        req.setTenantId(principal.getTenantId());
        userService.saveUserForm(req);
        return ResponseDTO.buildSuccessDataResp("ok");
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除用户")
    public ResponseDTO<String> removeUserList(@RequestBody Long[] id) {
        boolean idContains = contains(id, 1);
        if (idContains) {
            return ResponseDTO.buildError("不能删除超级管理员！");
        }
        userService.removeUserList(id);
        return ResponseDTO.buildSuccessDataResp("ok");
    }
    @PostMapping("/modify")
    @ApiOperation(value = "修改密码")
    public ResponseDTO<String> modify(@RequestBody UserReq req) {
        if (req.getId() == null) {
            return ResponseDTO.buildError("未找到用户");
        }
        userService.modify(req);
        return ResponseDTO.buildSuccessDataResp("ok");
    }

    @PostMapping("/modify/info")
    @ApiOperation(value = "修改信息")
    public ResponseDTO<String> modifyInfo(@RequestBody UserReq req) {
        if (req.getId() == null) {
            return ResponseDTO.buildError("未找到用户");
        }
        userService.modifyInfo(req);
        return ResponseDTO.buildSuccessDataResp("ok");
    }

    @PostMapping("/queryUserList")
    @ApiOperation(value = "获取用户列表")
    public List<UserQueryResp> queryUserList(@RequestBody UserQueryReq req) {
        return userService.queryUserList(req);
    }

    @PostMapping("/resetPwd")
    @ApiOperation(value = "重置密碼")
    public ResponseDTO<String> resetPwd(@RequestBody UserReq req) {
        if (req.getId() == null) {
            return ResponseDTO.buildError("未找到用户");
        }
        if (StringUtils.isEmpty(req.getPassword())) {
            req.setPassword("123456");
        }
        userService.resetPwd(req);
        return ResponseDTO.buildSuccessDataResp("ok");
    }

    public static boolean contains(Long[] array, long value) {
        for (long v : array) {
            if (v == value) {
                return true;
            }
        }
        return false;
    }
}
